'use strict';

const BaseController = require('./base');
const serviceName = 'product';

class ProductController extends BaseController {
  constructor(ctx) {
    super(ctx, serviceName);
  }

  // 重写getPageQuery方法，支持按名称和分类搜索
  getPageQuery(query) {
    const where = {};
    if (query.name) {
      where.name = {
        $like: `%${query.name}%`
      };
    }
    if (query.categoryId) {
      where.categoryId = query.categoryId;
    }
    where.status = 1; // 只查询上架的商品
    return where;
  }

  // 检查指定日期的库存
  async checkDateStock() {
    const { ctx } = this;
    const { productId, date, personCount } = ctx.query;
    
    const hasStock = await ctx.service.product.checkStock(productId, date, parseInt(personCount));
    ctx.success({ hasStock });
  }
}

module.exports = ProductController;
